import 'package:flutter_test/flutter_test.dart';
import 'package:sow_app/repository/ajax_rest_client.dart';
import 'package:sow_app/repository/ajax_user_repository.dart';
import 'package:sow_app_core/sow_app_core.dart';

void main() async {
  group('user repository share common token test group', () {
    AjaxRestClient client = AjaxRestClient();
    test(
      "should return sucessful AjaxResponseModel when invoke register()",
      () async {
        // arrange
        UserRepository repository = AjaxUserRepository(client);
        try {
          // act
          await repository.register("17612345678", "17612345678", 1234);
          // assert
        } on ServerException catch (e) {
          expect(e.msg, '验证码错误！');
        }
      },
    );
    test(
      "should return TokenResponseModel when invoke login()",
      () async {
        // arrange

        UserRepository repository = AjaxUserRepository(client);
        // act
        var result = await repository.login("13560421324", "123456");
        // assert
        expect(result, isA<String>());
        expect(result, isNotEmpty);
        client.setAuthorization(result);
      },
    );
    test(
      "should return UserInfoModel when invoke info()",
      () async {
        // arrange
        UserRepository repository = AjaxUserRepository(client);
        // act
        var result = await repository.info();
        // assert
        expect(result, isA<User>());
        expect(result.userId, greaterThan(1));
        expect(result.userName, '13560421324');
        expect(result.nickName, '13560421324');
      },
    );
    test(
      "should return UserNotfoundException when invoke info() that token is empty",
      () async {
        // arrange
        UserRepository repository = AjaxUserRepository(client);
        // act
        client.setAuthorization('');
        try {
          var result = await repository.info();
          // assert
          expect(result, throwsException);
        } catch (e) {
          expect(e, isA<UserNotfoundException>());
        }
      },
    );
  });
}
